package com.cisau.service.impl;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;

import com.cisau.entity.GoodsEntity;
import com.cisau.entity.PrizeEntity;
import com.cisau.mapper.GoodsMapper;
import com.cisau.service.GoodsService;
import com.cisau.util.FileUtil;

@Service
public class GoodsServiceImpl implements GoodsService {

	@Resource
	GoodsMapper goodsMapper;

	@Override
	public void addGoods(GoodsEntity goods, HttpServletRequest request) {
		// TODO Auto-generated method stub
		
			String pic = FileUtil.upload(goods.getUploadFile(), request);
			goods.setPicpath(pic);
			goodsMapper.addGoods(goods);

	}

	@Override
	public List<GoodsEntity> findAll(HttpServletRequest request) {
		// TODO Auto-generated method stub
		return goodsMapper.findAll();
	}

	@Override
	public void findOneById(HttpServletRequest request) {
		// TODO Auto-generated method stub
		int id = Integer.parseInt(request.getParameter("id"));
		GoodsEntity goods =goodsMapper.findOneById(id);
		request.setAttribute("goods", goods);
	}

	@Override
	public void update(GoodsEntity goods, HttpServletRequest request) {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
				if (goods.getUploadFile().getSize() > 0) {
					FileUtil.deleteFile(request, goods.getPicpath());
					String pic = FileUtil.upload(goods.getUploadFile(), request);
					goods.setPicpath(pic);
				}
				
					goodsMapper.update(goods);
				
	}
}
